﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using Business;

namespace BaseWeb.JQuery.方法汇聚
{
    public partial class _4 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { 
                BindMenu("0");
            }
        }

        /// <summary>
        /// 生成按钮
        /// </summary>
        private void BindMenu(string parentKeyId)
        {
            List<li_Menu> arrayMenu = CommonSystem.Instance.Service.GetListByWhere<li_Menu>(c => c.ParentKeyId == parentKeyId).OrderBy(c => c.OrderIndex).ToList();
            //生成面板
            System.Web.UI.HtmlControls.HtmlGenericControl divPanel = new System.Web.UI.HtmlControls.HtmlGenericControl() { ID = "div_" + parentKeyId };

            #region 不用代码
            //if (parentKeyId == this.hid_Current.Value)
            //{
            //    //divPanel.Attributes.Add("class", "panel_show");
            //    divPanel.Style["display"] = "";
            //}
            //else
            //{
            //    //divPanel.Attributes.Add("class", "panel_hid");
            //    divPanel.Style["display"] = "none";
            //}
            #endregion

            divBody.Controls.Add(divPanel);
            foreach (var enMenu in arrayMenu)
            {
                System.Web.UI.HtmlControls.HtmlGenericControl li = new System.Web.UI.HtmlControls.HtmlGenericControl();
                li.TagName = "li";
                li.InnerText = enMenu.Name;
                li.Attributes.Add("class", "ui-state-default");
                if (GetChildCount(enMenu.KeyId) == 0)
                {
                    string jstring = string.Format("InApp('" + enMenu.KeyId + "')");
                    li.Attributes.Add("ondblclick", jstring);
                }
                else
                {
                    string jstring = string.Format("InChildMenu('" + enMenu.KeyId + "')");
                    li.Attributes.Add("ondblclick", jstring);
                }

                li.ID = "li_" + enMenu.KeyId;
                divPanel.Controls.Add(li);

                BindMenu(enMenu.KeyId);
            }
        }

        public int GetChildCount(string parentKeyId)
        {
            return CommonSystem.Instance.Service.GetListByWhere<li_Menu>(c => c.ParentKeyId == parentKeyId).Count();
        }

    }
}
